package com.alibobo.tech5.ganji;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.alibobo.utils.calendar.YMDHMS;
import com.alibobo.utils.file.FileUtils;
import com.alibobo.utils.json.JsonUtils;

public class CalendarHelper {

	protected static CalendarHelper _instance = new CalendarHelper();
	protected static Hashtable<String, JSONObject> _cache = new Hashtable<String, JSONObject>();

	protected static JSONObject getMonthData(int y, int m) {
		String fn = y + "-" + m;
		if (_cache.containsKey(fn))
			return _cache.get(fn);

		String p = "com/alibobo/tech5/ganji/data/calendar/date/";

		String file = p + fn + ".json";
		InputStream in = _instance.getClass().getClassLoader()
				.getResourceAsStream(file);
		if (in == null)
			return null;
		String content = FileUtils.readInputStreamAsString(in);
		try {
			JSONObject jo = new JSONObject(content);
			_cache.put(fn, jo);
			return jo;
		} catch (Exception e) {

		} finally {
			try {
				in.close();
			} catch (Exception e2) {
			}
		}
		return null;
	}

	public static List<String> getDayGanJi(int y, int m, int d) {
		JSONObject mon = getMonthData(y, m);
		if (mon == null)
			return null;
		String key = String.format("%d/%d/%d", y, m, d);
		List<String> result = new ArrayList<String>();
		try {
			JSONObject dj = mon.getJSONObject(key);
			JSONArray ja = dj.getJSONArray("gj");
			for (int i = 0; i < 3; i++) {
				String s = ja.getString(i);
				result.add(GJR.fixGanJi(s));
			}
		} catch (Exception e) {

		}
		return result;
	}

	/************************************************
	 * 月將一定要從 占時 來
	 * 
	 * @param dt
	 * @return
	 ***********************************************/
	protected static Hashtable<String, List<Hashtable<String, Object>>> _jachiCache = new Hashtable<String, List<Hashtable<String, Object>>>();

	public static List<Hashtable<String, Object>> getJachidOfYear(int y) {

		String key = String.valueOf(y);
		if (_jachiCache.contains(key))
			return _jachiCache.get(key);
		String fn = "com/alibobo/tech5/ganji/data/calendar/jachi/" + key
				+ ".json";
		JSONArray arr = JsonUtils.loadJSONArrayFromClassPath(fn);
		int sz = arr.length();
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		List<Hashtable<String, Object>> result = new ArrayList<Hashtable<String, Object>>();
		try {

			for (int i = 0; i < sz; i++) {
				Hashtable<String, Object> t = new Hashtable<String, Object>();
				JSONObject obj = arr.getJSONObject(i);

				t.put("name", obj.getString("name"));
				t.put("type", obj.getString("type"));
				t.put("id", obj.getString("id"));
				t.put("mg", obj.getString("mg"));

				List<String> ss = JsonUtils.readStringArrayFrom(obj
						.getJSONArray("gj"));
				List<String> gj = new ArrayList<String>();
				for (String s : ss) {
					gj.add(GJR.fixGanJi(s));
				}
				t.put("gj", gj);
				String ts = obj.getString("time");
				ts = ts.replace("T", " ");
				Date d1 = format1.parse(ts);
				t.put("time", d1);
				result.add(t);
			}
		} catch (Exception e) {

		}
		_jachiCache.put(key, result);
		return result;
	}

	public static String getMonGeneral(Date dt) {

		YMDHMS t = new YMDHMS(dt);
		List<Hashtable<String, Object>> jachis = getJachidOfYear(t.year);
		// 應該倒數~~
		int sz = jachis.size();
		for (int i = sz - 1; i >= 0; i--) {
			Hashtable<String, Object> ht = jachis.get(i);
			if (ht.get("type").equals("氣")) {
				if (dt.compareTo((Date) ht.get("time")) > 0) {
					return (String) ht.get("mg");
				}
			}
		}
		return "";
	}

	public static List<String> getDayGanJi(Date dt) {

		
		YMDHMS ymd=new YMDHMS(dt);
		return getDayGanJi(ymd.year,ymd.month,ymd.day);
	}

	public static String getCurrentJi(Date dt) {

		YMDHMS t = new YMDHMS(dt);
		int hr = t.hour;
		hr = hr + 2;
		int id = hr / 2 + hr % 2;
		return GJR.id2Ji(id);
	}

	public static String getCurrentJi() {
		return getCurrentJi(new Date());
	}
}
